<template>
  <div class="app-container">
    <!-- <div class="filter-container bor-container">
      <el-form size="mini" :model="formInline" :inline="true">
        <el-form-item prop="name">
          <el-input v-model="formInline.name" placeholder="请输入角色名"></el-input>
        </el-form-item>
        <el-button size="mini" @click="query()" type="primary" icon="el-icon-search">查询</el-button>
      </el-form>
    </div> -->
    <div class="filter-container btn-right">
      <el-button size="mini"  type="primary" @click="dialog.status = true" icon="el-icon-plus">添加礼物</el-button>
    </div>
    <el-table v-loading="loading" border :data="tableData">
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column prop="prizename" label="礼物名称">
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间">
      </el-table-column>
      <el-table-column prop="remark" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" size="mini" v-if="scope.row.status === 0">上架</el-tag>
          <el-tag type="danger" size="mini" v-else-if="scope.row.status === 1">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button  size="mini" type="warning" @click="openEdit(scope.row)" icon="el-icon-edit-outline" >编辑</el-button>
          <el-button  size="mini" type="warning" v-if="scope.row.status === 0" @click="Obtained(scope.row)" icon="el-icon-edit-outline" >下架</el-button>
          <el-button  size="mini" type="warning" v-else-if="scope.row.status === 1" @click="Obtained(scope.row)" icon="el-icon-edit-outline" >上架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-right"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.page"
      :page-sizes="[10]"
      :page-size="formInline.pagenum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.total">
    </el-pagination>
    <gift-list-edit
      @close="close"
      @success="success"
      v-if="dialog.status"
      :id="dialog.id"
      :dialog_="dialog.status"
    ></gift-list-edit>
  </div>
</template>
<script>
  import { getGiftSystemListAPI , obtainedGiftSystemAPI} from '@/api/gift'
  import { mapGetters } from 'vuex'
  import GiftListEdit from './components/GiftListEdit'
  export default {
    // 角色管理
    name: 'GiftList',
    components: {
      GiftListEdit
    },
    data() {
      return {
        formInline: {
          name: '',
          page: 0,
          pagenum: 10,
          total: 0
        },
        dialog: {
          status: false,
          id: null
        },
        loading: false,
        tableData: [],
      }
    },
    computed: {
      ...mapGetters([
        'buttons',
      ])
    },
    methods: {
      // 打开编辑页面
      openEdit(obj) {
        this.dialog.id = obj.id
        this.dialog.status = true
      },
      // 操作成功更新列表
      success() {
        this.getGiftList()
      },
      close() {
        this.dialog.status = false
        this.dialog.id = null
      },
      // 下架 或者 上架
      Obtained(obj){
        debugger
        if(obj.status == 1){ // 如果点击的是上架，则不提示弹窗
          obj.delLoading = true
          obtainedGiftSystemAPI({ id: obj.id,status:0}).then(res => {
            obj.delLoading = false
            if (res.code === 0) {
              this.$emit('success')
              this.$message.success('操作成功')
              this.getGiftList()
            } else {
              this.$message.error("操作失败")
            }
          })
        }else{
          this.$confirm('是否确认下架该礼物?下架后将不在前端显示', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let status = 0;
            if(obj.statustype == 1){ // 上架
              status = 0
            }else{
              status = 1
            }
            obj.delLoading = true
            obtainedGiftSystemAPI({ id: obj.id,status:status}).then(res => {
              obj.delLoading = false
              if (res.code === 0) {
                this.$emit('success')
                this.$message.success('操作成功')
                this.getGiftList()
              } else {
                this.$message.error("操作失败")
              }
            })
          }).catch(() => {
          });
        }

      },
      // 删除角色
      onDelete(obj, index) {
        this.$confirm('是否删除该banner图?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          debugger
          obj.delLoading = true
          deleteRoleAPI({ id: obj.Id}).then(res => {
            obj.delLoading = false
            if (res.code === 0) {
              this.$emit('success')
              this.$message.success('操作成功')
              this.getGiftList()
            } else {
              this.$message.error(res.message)
            }
          })
        }).catch(() => {
        });
      },
      // 点击查询事件
      query() {
        this.formInline.pageNum = 1
        this.getGiftList()
      },
      // pageSize发生改变触发
      handleSizeChange(val) {
        this.formInline.pageNum = 1
        this.formInline.pageSize = val
        this.getGiftList()
      },
      // pageNum 发生改变触发
      handleCurrentChange(val) {
        this.formInline.page = val
        this.getGiftList()
      },
      // 获取列表
      getGiftList() {
        this.loading = true
        getGiftSystemListAPI(this.formInline).then(res => {
          this.loading = false
          if (res.code === 0) {
            this.tableData = res.data.list
            this.formInline.total = res.data.total;
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    mounted() {
      this.getGiftList()
    }
  }
</script>

